//
//  rechargeShowView.m
//  tcpoctest
//
//  Created by apple on 13-7-2.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "rechargeShowView.h"
#import "purchaseView.h"

#import <QuartzCore/QuartzCore.h>

@implementation rechargeShowView

-(id)init
{
    self = [super init];
    
    if (self != nil) {
        isendcount = 1;
    }
    
    return self;
}

-(void)initUI
{
    UIView *toptapview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, 100)];
    UITapGestureRecognizer *singleTouch = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                  action:@selector(viewTap)];
    [self.view addSubview:toptapview];
    [toptapview addGestureRecognizer:singleTouch];
    
    UIView *bottomtapview = [[UIView alloc] initWithFrame:CGRectMake(0, 340, WINDOW_WIDTH, WINDOW_HIGTH - 340)];
    UITapGestureRecognizer *singleTouchbottom = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                        action:@selector(viewTap)];
    [self.view addSubview:bottomtapview];
    [bottomtapview addGestureRecognizer:singleTouchbottom];
    
    UIImageView *bgview = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 300, 240)];
    bgview.image = [UIImage imageNamed:@"gift_dialogbox_bg.png"];
    bgview.userInteractionEnabled = YES;
    [self.view addSubview:bgview];
    
    UIView  *TextTapView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 240)];
    
    UITapGestureRecognizer *singleTouchbg = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(bgviewTap)];
    [TextTapView addGestureRecognizer:singleTouchbg];
    
    [bgview addSubview:TextTapView];
    
    UIButton    *sendbtn = [[UIButton alloc] initWithFrame:CGRectMake(25, 170, 80, 40)];
    [sendbtn setBackgroundImage:[UIImage imageNamed:@"gift_button_cancel.png"]
                       forState:UIControlStateNormal];
    [sendbtn setBackgroundImage:[UIImage imageNamed:@"gift_button_cancel_on.png"]
                       forState:UIControlStateHighlighted];
    [sendbtn addTarget:self action:@selector(sendclick) forControlEvents:UIControlEventTouchUpInside];
    [sendbtn setTitle:@"购买" forState:UIControlStateNormal];
    [sendbtn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [bgview addSubview:sendbtn];
    
    UIButton    *canclebtn = [[UIButton alloc] initWithFrame:CGRectMake(195, 170, 80, 40)];
    [canclebtn setBackgroundImage:[UIImage imageNamed:@"gift_button_cancel.png"]
                         forState:UIControlStateNormal];
    [canclebtn setBackgroundImage:[UIImage imageNamed:@"gift_button_cancel_on.png"]
                         forState:UIControlStateHighlighted];
    [canclebtn addTarget:self action:@selector(cancleclick) forControlEvents:UIControlEventTouchUpInside];
    [canclebtn setTitle:@"取消" forState:UIControlStateNormal];
    [bgview addSubview:canclebtn];
    
    headimageview = [[UIImageView alloc] initWithFrame:CGRectMake(40, 60, 24, 24)];
    headimageview.image = [UIImage imageNamed:@"gold.png"];
    [bgview addSubview:headimageview];
    
    namelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 300, 20)];
    namelabel.textAlignment = UITextAlignmentCenter;
    namelabel.backgroundColor = [UIColor clearColor];
    namelabel.textColor = [UIColor whiteColor];
    [bgview addSubview:namelabel];
    
    warnlabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 40, WINDOW_WIDTH - 110, 60)];
    warnlabel.textAlignment = UITextAlignmentCenter;
    warnlabel.backgroundColor = [UIColor clearColor];
    warnlabel.textColor = [UIColor whiteColor];
    warnlabel.numberOfLines = 0;
    [bgview addSubview:warnlabel];
    
    UIImageView *inputBgView = [[UIImageView alloc]initWithFrame:CGRectMake(110, 120, 80, 30)];
    inputBgView.image = [UIImage imageNamed:@"cell.png"];
    inputBgView.userInteractionEnabled = YES;
    [bgview addSubview:inputBgView];
    
    inputTF = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 80, 30)];
    inputTF.font = CENTER_CELL_CONTENT_FONT;
    inputTF.textColor=[UIColor blackColor];
    inputTF.borderStyle=UITextBorderStyleNone;
    inputTF.leftViewMode = UITextFieldViewModeAlways;
    inputTF.placeholder = [NSString stringWithFormat:@"%d",isendcount];
    inputTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    inputTF.delegate =self;
    inputTF.keyboardType = UIKeyboardTypeNumberPad;
    [inputBgView addSubview:inputTF];
    
    UIButton *addcountbtn = [[UIButton alloc] initWithFrame:CGRectMake(210, 118, 30, 30)];
    [addcountbtn setTitle:@"+" forState:UIControlStateNormal];
    [addcountbtn addTarget:self action:@selector(addcount) forControlEvents:UIControlEventTouchUpInside];
    
    [addcountbtn setBackgroundColor: [UIColor blueColor]];
    [addcountbtn.layer setMasksToBounds:YES];
    [addcountbtn.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [addcountbtn.layer setBorderWidth:1.0];   //边框宽度
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
    [addcountbtn.layer setBorderColor:colorref];
    
    [bgview addSubview:addcountbtn];
    
    UIButton *subcountbtn = [[UIButton alloc] initWithFrame:CGRectMake(60, 118, 30, 30)];
    [subcountbtn setTitle:@"--" forState:UIControlStateNormal];
    [subcountbtn addTarget:self action:@selector(subcount) forControlEvents:UIControlEventTouchUpInside];
    
    [subcountbtn setBackgroundColor: [UIColor blueColor]];
    [subcountbtn.layer setMasksToBounds:YES];
    [subcountbtn.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [subcountbtn.layer setBorderWidth:1.0];   //边框宽度
    [subcountbtn.layer setBorderColor:colorref];
    
    [bgview addSubview:subcountbtn];
}

-(void)viewTap
{
    if ([inputTF isFirstResponder]) {
        [inputTF resignFirstResponder];
        
        isendcount = [inputTF.text intValue];
        
        if (isendcount <= 0) {
            isendcount = 1;
        }
        
        [self refreshText];
    }
    else
    {
        [[ViewManager sharedViewManager] backViewDirect:self];
    }
}

-(void)bgviewTap
{
    if ([inputTF isFirstResponder]) {
        [inputTF resignFirstResponder];
        
        isendcount = [inputTF.text intValue];
        
        if (isendcount <= 0) {
            isendcount = 1;
        }
        
        [self refreshText];
    }
}

-(void)refreshText
{
    namelabel.text = [NSString stringWithFormat:@"购买 %d 个",isendcount];
    
    warnlabel.text = [NSString stringWithFormat:@"%d ￥可购买%d万金币耶",
                      isendcount * 6,isendcount * 6];
    
    inputTF.text = [NSString stringWithFormat:@"%d",isendcount];
}

-(void)cancleclick
{
    [[ViewManager sharedViewManager] backViewDirect:self];
}

-(void)addcount
{
    isendcount++;
    
    [self refreshText];
}

-(void)subcount
{
    isendcount--;
    
    if (isendcount <= 0) {
        isendcount = 1;
    }
    
    [self refreshText];
}

-(void)refreshView
{
        namelabel.text = [NSString stringWithFormat:@"购买 %d 个",isendcount];
        warnlabel.text = [NSString stringWithFormat:@"%d ￥可购买%d万金币耶",
                          isendcount * 6,isendcount * 6 * 10];
    
    [self refreshText];
}

-(void)viewWillShow
{
    isendcount = 1;
    
    [self refreshView];
}

-(void)sendclick
{
    [[ViewManager sharedViewManager] backViewDirect:self];
    
    [ViewManager sharedViewManager].purchaseVC.ipaycount = isendcount;
    [[ViewManager sharedViewManager].purchaseVC topay];
}

@end
